exports.add = function(req,res){

  //获取参数信息
  var bodyParams = req.routeInfo.bodyParams;
    
  //新增成功的节点ID
  var osid = 0;

  //新增用户ID
  var userid = 0;

  //判断该手机号是否已经被使用
  function validationPhone(cb){

      R.SEND_HTTP(
          req,
          res,
          {
              url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_username/" + bodyParams.os_phone,
              method : 'GET'
          },
          function (data) {

              if(data.result.data.length > 0){
                 return RES.errorResponse(res,"该手机号已被录入，请勿重复录入！");
              }

              cb();

          }
      )


  }

  //新增代理商节点
  function addOrganization(cb){

      //节点路径
      var os_path = "0,1," + bodyParams.os_id + ",";

      R.SEND_HTTP(
          req,
          res,
          {
              url: CORE_APIConfig.coreAPI.organizationalStructureAdd.Request_Api_Url,
              method : 'POST',
              data   : {
                  os_name        : bodyParams.os_name,
                  os_path        : os_path,
                  os_fid         : bodyParams.os_id,
                  os_is_inherit  : 1,
                  os_master_type : 2
              }
          },
          function (data) {

              osid = data.result;

              cb();
              
          }
      )

  }

  //更新节点信息表中的非自建节点路径
  function updateOrganizationPath(cb){

      //子节点路径
      var os_child_path = bodyParams.os_id + "," + osid + ","

      //子节点类型
      var os_child_type = 1


      R.SEND_HTTP(
          req,
          res,
          {
              url: CORE_APIConfig.coreAPI.organizationalStructureUpdate.Request_Api_Url,
              method : 'POST',
              data   : {
                  update_where   : "os_id",
                  os_id          : osid,
                  os_child_path  : os_child_path,
                  os_child_type  : os_child_type
              }
          },
          function (data) {

              cb();
              
          }
      )

  }

  //新增节点扩展信息
  function addOrganizationExtensionInfo(cb){

      R.SEND_HTTP(
          req,
          res,
          {
              url: CORE_APIConfig.coreAPI.artOrganizationStructureExtensionInfoAdd.Request_Api_Url,
              method : 'POST',
              data   : {
                  osei_phone       : bodyParams.os_phone,
                  osei_create_time : common.nowTime(false),
                  osei_osid        : osid,
                  osei_contact     : bodyParams.os_name
              }
          },
          function (data) {

              cb();
              
          }
      )
  }

  //创建管理员账户
  function createManagerUser(cb){

      R.SEND_HTTP(
          req,
          res,
          {
              url: CORE_APIConfig.coreAPI.userAdd.Request_Api_Url,
              method : 'POST',
              data   : {
                  u_username      : bodyParams.os_phone,
                  u_password      : '123456',
                  u_nickname      : bodyParams.os_name,
                  u_phone         : bodyParams.os_phone,
                  u_create_time   : common.nowTime(false),
                  u_is_activation : 1,
                  u_type          : 2,
                  u_master_osid   : osid,
                  u_is_manager    : 1
              }
          },
          function (data) {

              userid = data.result;

              cb();
          }
      )
  }

  //将管理员添加进该组织结构节点
  function userToNode(cb){

      R.SEND_HTTP(
          req,
          res,
          {
              url: CORE_APIConfig.coreAPI.userOrganizationalMiddleAdd.Request_Api_Url,
              method : 'POST',
              data   : {
                  uom_osid : osid,
                  uom_uid  : userid
              }
          },
          function (data) {
              cb();
          }
      )
  }

  //将角色分配给新创建的节点
  function roleToNode(cb){

      R.SEND_HTTP(
          req,
          res,
          {
              url: CORE_APIConfig.coreAPI.organizationalRoleMiddleAdd.Request_Api_Url,
              method : 'POST',
              data   : {
                  orm_osid  : osid,
                  orm_orid  : bodyParams.os_master_type == 1 ? 38 : 39
              }
          },
          function (data) {
              cb();
          }
      )
  }

  F.Serial([
    validationPhone,
    addOrganization,
    updateOrganizationPath,
    addOrganizationExtensionInfo,
    createManagerUser,
    userToNode,
    roleToNode
  ],() => {
      RES.successResponse(res);
  })

}